﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///JGXXcs 的摘要说明
/// </summary>
public partial class JGXX
{
    public enum typeJG { 个人 = 1, 企业, 民办 }
    public enum sortJG { 培训学校 = 1, 幼儿园 }
    public enum statusRZ { 未实名认证 = 0, 认证中, 认证通过, 认证不通过 }
    public enum levelRZ { 资格认证 = 1, 资质认证 }
    public static JGXX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JGXX.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<JGXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.JGXX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.JGXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<JGXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.JGXX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.JGXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            JGXX objJGXX = JGXX.GetByID(ID);
            if (objJGXX == null)
                return;
            objJGXX.Enable_Flag = 0;
            objJGXX.Update();
        }
    }

    public static List<JGXX> GetAllByCondition(int shengID, int shiID, int quID, string address,string jgName,string des,string jgGM, int jgType, int jgSort, int rzLevel, int rzStatus)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.JGXX
                        where t.Enable_Flag == 1
                        select t;
            if (shengID != 0)
            {
                query = query.Where(row => row.ProvinceID == shengID);
            }
            if (shiID != 0)
            {
                query = query.Where(row => row.CityID == shiID);
            }
            if (quID != 0)
            {
                query = query.Where(row => row.ReginIDs == quID.ToString());
            }
            if (!string.IsNullOrEmpty(address))
            {
                query = query.Where(row => row.Address.Contains(address));
            }
            if (!string.IsNullOrEmpty(jgName))
            {
                query = query.Where(row => row.JFName.Contains(jgName));
            }
            if (!string.IsNullOrEmpty(des))
            {
                query = query.Where(row => row.Des.Contains(des));
            }
            if (!string.IsNullOrEmpty(jgGM))
            {
                query = query.Where(row => row.JFGM.Contains(jgGM));
            }
            if (jgType != 999)
            {
                query = query.Where(row => row.Type == jgType);
            }
            if (jgSort != 999)
            {
                query = query.Where(row => row.Sort == jgSort);
            }
            if (rzLevel != 999)
            {
                query = query.Where(row => row.RZLEVEL == rzLevel.ToString());
            }
            if (rzStatus != 999)
            {
                query = query.Where(row => row.SMRZStatus == rzStatus);
            }
            return query.OrderByDescending(row => row.ID).ToList();
        }
        //using (DataClassesDataContext context = new DataClassesDataContext())
        //{
        //    if (shengID != 0)
        //    {
        //        //query = query.Where(row => row.ProvinceID == shengID);
        //    }
        //    object[] pams = { 3, "%管理员" };

        //    var query = context.ExecuteQuery<JGXX>(@"Select ID, RoleName from dbo.AdminRole where RoleName  LIKE {1} and ID={0} ", pams);
        //    return query.ToList();
        //}

    }
}